#ifndef ELINPUT_H
#define ELINPUT_H

#include "../basic/elcomponent.h"

class QLineEdit;
class ElTextButton;

class ElInput : public ElComponent
{
    Q_OBJECT
public:
    ElInput(QWidget *parent = nullptr);
    ElInput(const QString &text, QWidget *parent = nullptr);

    QLineEdit *editor() const;

    int maxLength() const;
    void setMaxLength(int l);

    int minLength() const;
    void setMinLength(int l);

    QWidget *prepend() const;
    void setPrepend(QWidget *prepend);

    QWidget *append() const;
    void setAppend(QWidget *append);

    QWidget *suffix() const;
    void setSuffix(QWidget *suffix);

    QWidget *prefix() const;
    void setPrefix(QWidget *prefix);

    QString text() const;
    void setText(const QString &text);

    bool showWordLimit() const;
    void setShowWordLimit(bool b);

    bool showPwdIcon() const;
    void setShowPwdIcon(bool b);

    bool isAutoFocus() const;
    void setAutoFocus(bool autoFocus);

protected:
    bool eventFilter(QObject *watched, QEvent *e) override;
    bool event(QEvent *e) override;
    void paintEvent(QPaintEvent *e) override;
    void enterEvent(QEnterEvent *e) override;
    void leaveEvent(QEvent *e) override;
    void initPalette() override;

private:
    Q_PROPERTY(int minLength READ minLength WRITE setMinLength)
    Q_PROPERTY(int maxLength READ maxLength WRITE setMaxLength)
    Q_PROPERTY(QString text READ text WRITE setText)
    Q_PROPERTY(bool autoFocus READ isAutoFocus WRITE setAutoFocus)
    Q_PROPERTY(bool showWordLimit READ showWordLimit WRITE setShowWordLimit)
    Q_PROPERTY(bool showPwdIcon READ showPwdIcon WRITE setShowPwdIcon)

    static const int Init_Width;
    static const int Init_Height;
    static const int Padding_Lr;
    static const int Padding_Tb;
    static const int Prefix_Spacing;
    static const int Suffix_Spacing;

    QLineEdit *_lineEdit;
    int _minLen;
    bool _autoFocus = true;
    bool _showWordLimit = false;
    bool _showPwdIcon = false;
    QWidget *_prepend = nullptr;
    QWidget *_append = nullptr;
    QWidget *_suffix = nullptr;
    QWidget *_prefix = nullptr;

    ElTextButton *_pwdBtn = nullptr;
    bool _focusIn = false; // used for the visibility of pwdBtn

    void createPwdBtn();
};

#endif // ELINPUT_H
